﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace MP42FLV
{
    public partial class Form1 : Form
    {
        string ffmpeg_path = Environment.CurrentDirectory + @"\ffmpeg.exe";

        public Form1()
        {
            InitializeComponent();
        }

        private void AddFile(object sender, EventArgs e)
        {
            if (DialogResult.OK == AddFileDialog.ShowDialog())
            {
                foreach (var item in AddFileDialog.FileNames)
                {
                    AddItems(item);
                }
            }
        }

        private void AddFolder(object sender, EventArgs e)
        {
            if (DialogResult.OK == AddFolderDialog.ShowDialog())
            {
                GetItems(AddFolderDialog.SelectedPath);
            }
        }

        private void Remove_Click(object sender, EventArgs e)
        {
            int SelectedItems = FileLists.SelectedItems.Count;
            if (SelectedItems > 0)
            {
                for (int i = 0; i < SelectedItems; i++)
                {
                    FileLists.Items.Remove(FileLists.SelectedItems[0]);
                }
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            FileLists.Items.Clear();
        }

        private void Convert_Click(object sender, EventArgs e)
        {
            if (File.Exists(ffmpeg_path))
            {
                foreach (var item in FileLists.Items)
                {
                    if (!File.Exists(ChangeExtension(item.ToString())))
                    {
                        Convert(item);
                    }
                    else
                    {
                        if (DialogResult.Yes == MessageBox.Show("文件已存在是否覆盖。", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                        {
                            File.Delete(ChangeExtension(item.ToString()));
                            Convert(item);
                        }
                    }
                }
            }

        }

        private void Quit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddItems(string item)
        {
            if (!FileLists.Items.Contains(item) && Path.GetExtension(item) == ".mp4")
            {
                FileLists.Items.Add(item);
            }
        }

        void GetItems(string path)
        {

            foreach (var item in Directory.GetFiles(path))
            {
                AddItems(item);
            }

            foreach (var item in Directory.GetDirectories(path))
            {
                GetItems(item);
            }
        }

        private void Convert(object item)
        {
            var p = new Process();
            p.StartInfo.FileName = ffmpeg_path;
            p.StartInfo.Arguments = "-i " + '"' + item + '"' + " -vcodec copy -acodec copy " + " " + '"' + ChangeExtension(item.ToString()) + '"';
            p.Start();
            p.WaitForExit();
        }

        private static string ChangeExtension(string path)
        {
            return Path.ChangeExtension(path, ".flv");
        }
    }
}
